package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.renwei.yunlong.R;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.StringUtils;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.adapter
 * Created by :  Benjamin.
 * Created time: 2017/11/13 19:52
 * Changed by :  Benjamin.
 * Changed time: 2017/11/13 19:52
 * Class description:
 */

public class ImagePagerAdapter extends PagerAdapter implements View.OnClickListener {

    ImagePagerClick imagePagerClick;
    private Context mContext;
    private ArrayList<String> paths;
    OnFileClicklistener clicklistener;

    public interface OnFileClicklistener {
        void onClick(String path);
    }


    public ImagePagerAdapter(ArrayList<String> paths, Context context, OnFileClicklistener clicklistener) {
        this.paths = paths;
        this.mContext = context;
        this.clicklistener = clicklistener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.file_down:
                // TODO 17/12/05
                break;
            default:
                break;
        }
    }

    public void setImagePagerClick(ImagePagerClick imagePagerClick) {
        this.imagePagerClick = imagePagerClick;
    }

    @Override
    public int getCount() {
        return paths == null ? 0 : paths.size();
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        final String path = paths.get(position);
        if (path.toLowerCase().endsWith(".png") || path.toLowerCase().endsWith(".jpg") || path.toLowerCase().endsWith(".gif")
                || path.toLowerCase().endsWith(".jpeg") || path.toLowerCase().endsWith(".bmp")) {
            View imageView = View.inflate(mContext, R.layout.item_photo_display, null);
            PhotoView photoView = imageView.findViewById(R.id.photo_view);
            Glide.with(mContext).load(path).error(R.mipmap.icon_file_blank).into(photoView);
            photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    imagePagerClick.closePager();
                }
            });
            LogUtil.e("图片地址是:" + path);
            container.addView(imageView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return imageView;
        } else {
            View fileView = View.inflate(mContext, R.layout.item_file_display, null);
            TextView mNameFile = fileView.findViewById(R.id.file_name);
            Button mDownFile = fileView.findViewById(R.id.file_down);
            int end = path.lastIndexOf("/");
            mNameFile.setText(String.valueOf(path.substring(end + 1, path.length())));
            mDownFile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (clicklistener != null) {
                        clicklistener.onClick(StringUtils.value(path));
                    }
                }
            });
            container.addView(fileView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            return fileView;
        }
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public interface ImagePagerClick {

        void closePager();
    }

}